package org.gdstash.ui.select;

import org.gdstash.db.DBSkill;
import org.gdstash.db.DBSkillTree;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;

public class SkillBonusPane extends AdjustablePanel {
    private static List<DBSkill> skills;
    private static List<DBSkill> masteries;
    private static DBSkill[] bonusSkills;
    private JComboBox<DBSkill> cbSkills;

    public static void loadSkills() {
        final String s = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SKILL_ALL");
        if (GDStashFrame.dbConfig == null || !GDStashFrame.dbConfig.gddbInit) {
            (SkillBonusPane.bonusSkills = new DBSkill[2])[0] = null;
            final DBSkill all = new DBSkill(s);
            SkillBonusPane.bonusSkills[1] = all;
            return;
        }
        SkillBonusPane.skills = DBSkill.getBonusSkills();
        SkillBonusPane.masteries = new LinkedList<DBSkill>();
        if (SkillBonusPane.skills == null) {
            (SkillBonusPane.bonusSkills = new DBSkill[2])[0] = null;
            final DBSkill all = new DBSkill(s);
            SkillBonusPane.bonusSkills[1] = all;
        } else {
            final List<DBSkillTree> masteries = DBSkillTree.getMasteryTrees();
            (SkillBonusPane.bonusSkills = new DBSkill[SkillBonusPane.skills.size() + masteries.size() + 2])[0] = null;
            final DBSkill all2 = new DBSkill(s);
            SkillBonusPane.bonusSkills[1] = all2;
            int pos = 2;
            for (final DBSkillTree tree : masteries) {
                final DBSkill mSkill = tree.getMasterySkill();
                if (mSkill != null) {
                    final Object[] args = {mSkill.getName()};
                    final String txt = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_ALL_SKILLS_MASTERY", args);
                    final DBSkill skill = new DBSkill(txt, mSkill.getSkillID());
                    SkillBonusPane.masteries.add(skill);
                    SkillBonusPane.bonusSkills[pos] = skill;
                    ++pos;
                }
            }
            for (final DBSkill skill2 : SkillBonusPane.skills) {
                SkillBonusPane.bonusSkills[pos] = skill2;
                ++pos;
            }
        }
    }

    static {
        SkillBonusPane.skills = null;
        SkillBonusPane.masteries = null;
        SkillBonusPane.bonusSkills = null;
        loadSkills();
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SKILL_BONUS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbSkills == null) {
            this.cbSkills = new JComboBox<DBSkill>(SkillBonusPane.bonusSkills);
        }
        this.cbSkills.setFont(fntCombo);
        this.cbSkills.setPreferredSize(dimMax);
        this.cbSkills.setMaximumSize(dimMax);
    }

    public void updateBonusSkills() {
        final DefaultComboBoxModel<DBSkill> model = new DefaultComboBoxModel<DBSkill>(SkillBonusPane.bonusSkills);
        this.cbSkills.setModel(model);
    }

    public void clear() {
        this.cbSkills.getModel().setSelectedItem(null);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        final int index = this.cbSkills.getSelectedIndex();
        criteria.bonusSkill = null;
        criteria.allSkills = false;
        criteria.masteryBonusSkills = false;
        if (index == 1) {
            criteria.allSkills = true;
        } else {
            final DBSkill skill = (DBSkill) this.cbSkills.getSelectedItem();
            if (skill != null) {
                criteria.bonusSkill = skill.getSkillID();
                if (index < SkillBonusPane.masteries.size() + 2) {
                    criteria.masteryBonusSkills = true;
                }
            }
        }
    }

    public SkillBonusPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbSkills));
        layout.setVerticalGroup(vGroup);
    }
}
